package com.atlin.project.service.impl;

import com.atlin.project.contant.CommonConstant;
import com.atlin.project.model.domain.Template;
import com.atlin.project.mapper.TemplateMapper;
import com.atlin.project.model.dto.template.TemplateQueryRequest;
import com.atlin.project.model.vo.TemplateVO;
import com.atlin.project.utils.SqlUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atlin.project.service.TemplateService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author atlin
 * @description 针对表【template(示例表)】的数据库操作Service实现
 * @createDate 2024-03-05 20:11:37
 */
@Service
public class TemplateServiceImpl extends ServiceImpl<TemplateMapper, Template>
        implements TemplateService {

    @Override
    public void validTemplate(Template template, boolean add) {

    }

    @Override
    public TemplateVO getTemplateVO(Template template, HttpServletRequest request) {
        TemplateVO templateVO = TemplateVO.objToVo(template);
        // todo other operation
        return templateVO;
    }

    /**
     * 获取查询包装类（用户根据哪些字段查询，根据前端传来的请求对象，得到 mybatis 框架支持的查询 QueryWrapper 类）
     *
     * @param templateQueryRequest
     * @return
     */
    @Override
    public QueryWrapper<Template> getQueryWrapper(TemplateQueryRequest templateQueryRequest) {
        QueryWrapper<Template> queryWrapper = new QueryWrapper<>();
        if (templateQueryRequest == null) {
            return queryWrapper;
        }
        Long id = templateQueryRequest.getId();
        String title = templateQueryRequest.getTitle();
        String content = templateQueryRequest.getContent();
        String sortField = templateQueryRequest.getSortField();
        String sortOrder = templateQueryRequest.getSortOrder();

        queryWrapper.like(StringUtils.isNotBlank(title), "title", title);
        queryWrapper.like(StringUtils.isNotBlank(content), "content", content);
        queryWrapper.eq(ObjectUtils.isNotEmpty(id), "id", id);
        queryWrapper.eq("isDelete", false);
        queryWrapper.orderBy(SqlUtils.validSortField(sortField),
                sortOrder.equals(CommonConstant.SORT_ORDER_ASC), sortField);

        // todo other operation
        return queryWrapper;
    }

    @Override
    public Page<TemplateVO> getTemplateVOPage(Page<Template> templatePage, HttpServletRequest request) {
        List<Template> templateList = templatePage.getRecords();
        Page<TemplateVO> templateVOPage = new Page<>(templatePage.getCurrent(), templatePage.getSize(), templatePage.getTotal());
        if (CollectionUtils.isEmpty(templateList)) {
            return templateVOPage;
        }
        // todo other operation

        // 填充信息
        List<TemplateVO> templateVOList = templateList.stream().map(template -> {
            TemplateVO templateVO = TemplateVO.objToVo(template);
            // todo other operation

            return templateVO;
        }).collect(Collectors.toList());
        templateVOPage.setRecords(templateVOList);
        return templateVOPage;
    }
}




